package com.javagi.config;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import javax.annotation.Resource;
import java.io.Serializable;
import java.net.UnknownHostException;

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {

    public RedisConfig() {

        System.out.println("RedisConfig容器启动初始化。。。");

    }
    @Resource
    private LettuceConnectionFactory lettuceConnectionFactory;

    @Bean
    public RedisTemplate<String, Serializable> redisTemplate(
            LettuceConnectionFactory redisConnectionFactory)
            throws UnknownHostException {
        RedisTemplate<String,Serializable> template = new RedisTemplate<String,Serializable>();
        /*通过在这里设置redis存储数据采取的序列化格式*/
        template.setKeySerializer(new StringRedisSerializer());

//        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
//
//        template.setHashKeySerializer(new StringRedisSerializer());// Hash key序列化
//
//        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());// Hash value序列化

        template.setConnectionFactory(redisConnectionFactory);
        return template;
    }


}
